yjx.controller('clientInfoController',['$rootScope','$scope','$fetch','$stateParams','$timeout','$state','modalService',function ($rootScope,$scope,$fetch,$stateParams,$timeout,$state,modalService) {
    $rootScope.tabCtrlName = $rootScope.tabCtrlNameArr[3];
     $scope.sexes = [
         {id:1,value:'男'},
         {id:2,value:'女'}
     ];
    $scope.checkedTags=[];
    $scope.editShow=true
    $scope.selectedTag = {};

    /*解决android中软键盘弹出 将页面压缩的问题*/
    var HEIGHT = $('body').height();
    $('.m-clientInfo').height(HEIGHT)

    $scope.selectSex= function (id,value) {
        if($scope.$parent.editClient){
            $scope.$parent.editClient.sex=value;
        }else{
            $scope.$parent.editClient={};
            $scope.$parent.editClient.sex=value;
        }
    }

    $scope.selectTag = function(id,name) {
        $scope.selectedTag.id = id;
        $scope.selectedTag.name = name;
    }

    $scope.showTagAct = function() {
        if(!$scope.tags){
            modalService.mainMsgInfo("暂时没有标签可选！")
            //return
        }else{
            if($scope.$parent.editClient) {
                $scope.selectedTag.id = $scope.$parent.editClient.tagId;
                $scope.selectedTag.name =$scope.$parent.editClient.tag;
            }
            $scope.addTag = true;
        }
    }

    $scope.addTagAct = function () {
        $scope.addTag = false;
            // console.log("addTag")
        //var length = $scope.$parent.editClient&& $scope.$parent.editClient.tags?$scope.$parent.editClient.tags.length:0;
        //$scope.$parent.editClient.tags = $scope.$parent.editClient&& $scope.$parent.editClient.tags?$scope.$parent.editClient.tags:[];
        $timeout(function(){
            console.log($scope.$parent.editClient);
            if(!$scope.$parent.editClient){
                $scope.$parent.editClient = {};
                $scope.$parent.editClient.tagId = $scope.selectedTag.id;
                $scope.$parent.editClient.tag = $scope.selectedTag.name;
            }else{
                $scope.$parent.editClient.tagId = $scope.selectedTag.id;
                $scope.$parent.editClient.tag = $scope.selectedTag.name
            };
            /*可以添加多个tag时*/
            //for(var i in $scope.tags) {
            //
            //    if(length > 0 ){
            //        var count = 0
            //        for (var j in $scope.$parent.editClient.tags) {
            //            count++;
            //            if($scope.tags[i].id == $scope.$parent.editClient.tags[j].id){
            //                // console.log($scope.tags[i].id  +"这是会break的&&&&" +$scope.$parent.editClient.tags[j].id)
            //                $scope.tags[i].checked = false;
            //                break;
            //            }else if($scope.tags[i].id != $scope.$parent.editClient.tags[j].id && $scope.tags[i].checked && count == length ){
            //
            //                // console.log($scope.tags[i].id  +"这是push进去的ID&&&&" +$scope.$parent.editClient.tags[j].id + $scope.tags[i].checked)
            //                //$scope.$parent.editClient.tags.push($scope.tags[i]);
            //                $scope.$parent.editClient.tags = [$scope.tags[i]];/*改过需求 用户tag只能有一个*/
            //                $scope.tags[i].checked = false;
            //            }
            //        }
            //    }else {
            //        if($scope.tags[i].checked ){
            //            //$scope.$parent.editClient.tags.push($scope.tags[i]);
            //            $scope.$parent.editClient.tags = [$scope.tags[i]];/*改过需求 用户tag只能有一个**/
            //            $scope.tags[i].checked = false;
            //        }
            //    }
            //}
        })

        /*这里还要考虑是否与原来的tag重复*/
    }

    $scope.deleteTagAct = function (id) {
        $scope.$parent.editClient.tagId = 0;
        $scope.$parent.editClient.tag = "";
        console.log($scope.tags)
        //for(var i in $scope.$parent.editClient.tags) {
        //    if($scope.$parent.editClient.tags[i].id == id ) {
        //        $scope.$parent.editClient.tags.splice(i,1);
        //        return
        //    }
        //}
    }
    /*确认按钮操作*/
    $scope.editAct = function () {
        /*数据合法性验证*/
        if(!$scope.$parent.editClient.customerName){
            modalService.mainMsgError("姓名必填")
            return
        }
        if(!$scope.$parent.editClient.age){
            modalService.mainMsgError("年龄必填")
            return
        }
        if(isNaN($scope.$parent.editClient.age)){
            modalService.mainMsgError("年龄必须是数字")
            return
        }
        //if(!$scope.$parent.editClient.email){
        //    modalService.mainMsgError("请输入正确格式的邮箱")
        //    return
        //}
        if(!$scope.editClient.telephone){
            modalService.mainMsgError("手机号码必填")
            return
        }
        if(!$scope.editClient.telephone.match("^[1][34578][0-9]{9}$")){
            modalService.mainMsgError("请输入正确格式的手机号码")
            return
        }
        if(!$scope.$parent.editClient.tag) {
            modalService.mainMsgError("必须为用户选择一个标签");
            return
        }
        $scope.selectedTags=[];
        //if($scope.$parent.editClient.tags){
        //    for(var i in $scope.$parent.editClient.tags) {
        //        $scope.selectedTags.push($scope.$parent.editClient.tags[i].id);
        //    }
        //
        //}
        /*邮箱不能重复*/
        if($scope.shared.showEdit && $scope.shared.isEdit) {
            editClient();
        }
        else if($scope.shared.showEdit && !$scope.shared.isEdit) {
            /*希望后端可以返回id*/
            insertCleint();
        }
    }

    /*编辑用户*/
    function editClient() {

        $fetch.fetch(
            '/customer/update',
            {
                params:{
                    userId:$rootScope.userId,
                    id:$scope.$parent.editClient.id,
                    customerName:$scope.$parent.editClient.customerName,
                    sex:$scope.$parent.editClient.sex,
                    age:$scope.$parent.editClient.age,
                    email:$scope.$parent.editClient.email,
                    tagId:$scope.$parent.editClient.tagId,
                    tag:$scope.$parent.editClient.tag,
                    telephone:$scope.$parent.editClient.telephone,
                    address:$scope.$parent.editClient.address,
                    cardId:$scope.$parent.editClient.cardId,
                    //tagIds:$scope.selectedTags?$scope.selectedTags.join(','):null,
                },
                type:'POST',
            }
        ).then(function(data){
                $timeout(function(){
                    for(var i in $scope.$parent.clientList){
                        if($scope.$parent.clientList[i].id == $scope.$parent.editClient.id){
                            $scope.$parent.clientList.splice(i,1,$scope.$parent.editClient);
                            history.back()
                            $scope.shared.showEdit = false;
                        }
                    }
                })
            }).catch(function(e){
                    modalService.mainMsgError(e.msg);
            })
    }
    /*新增用户*/
    function insertCleint(){
        if(!$scope.$parent.editClient.sex) {
            $scope.$parent.editClient.sex = $scope.sexes[0].value;
        }
        $fetch.fetch(
            '/customer/input',
            {
                params:{
                    userId:$rootScope.userId,
                    customerName:$scope.$parent.editClient.customerName,
                    sex:$scope.$parent.editClient.sex,
                    age:$scope.$parent.editClient.age,
                    email:$scope.$parent.editClient.email,
                    //wechatId:$scope.$parent.editClient.wechatId,
                    tagId:$scope.$parent.editClient.tagId,
                    tag:$scope.$parent.editClient.tag,
                    telephone:$scope.$parent.editClient.telephone,
                    address:$scope.$parent.editClient.address,
                    cardId:$scope.$parent.editClient.cardId,
                    //tagIds:$scope.selectedTags?$scope.selectedTags.join(','):null,
                },
                type:'POST',
            }
        ).then(function(data){
                $timeout(function(){
                    console.log(data);
                    if($scope.$parent.clientList ) {
                        $scope.$parent.clientList.push(data);
                    }else {
                        $scope.$parent.clientList = [data];
                    }
                    //console.log($scope.$parent.clientList);
                    history.back()
                    //$scope.shared.showEdit = false;
                })
            }).catch(function(e){
                modalService.mainMsgError(e.msg)
            })
    }
    /*获取tags标签列表*/
    function getTags(){
        $fetch.fetch(
            '/customer/tags'
        ).then(function(data){
                $timeout(function(){
                    $scope.tags=data;
                })
            }).catch(function(e){
                modalService.mainMsgError(e.msg);
            })
    }
    getTags();

}])